
#include "stdafx.h"
#include "childframehelp.h"

UINT WM_HELP_VIEWCREATED = RegisterWindowMessage(_T("WM_HELP_CREATEVIEW"));

DWORD WINAPI _threadHelpCreateView(LPVOID lpData)
{
	HWND hParent = (HWND)lpData;

	::OleInitialize(NULL);

	CHelpAdapterCreator AdapterCreator;
	CWebContainer *pWebContainer = new CWebContainer(&AdapterCreator);
	CClientRect rcClient(hParent);

	pWebContainer->Create(hParent, rcClient);	
	pWebContainer->CreateWebControl();

	::PostMessage(hParent, WM_HELP_VIEWCREATED, 0, (LPARAM)pWebContainer->m_hWnd);

	MSG msg;
	while (::GetMessage(&msg, NULL, 0, 0))
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}

	::OleUninitialize();

	return 0;
}


//////////////////////////////////////////////////////////////////////////
// CHelpExternalDispatchAdapter

CHelpExternalDispatchAdapter::CHelpExternalDispatchAdapter(CWebContainer* pWebContainer)
: CExternalDispatch(pWebContainer)
{
}

CHelpExternalDispatchAdapter::~CHelpExternalDispatchAdapter()
{
}

STDMETHODIMP CHelpExternalDispatchAdapter::GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
	HRESULT hr = NOERROR;
	return hr;
}

STDMETHODIMP CHelpExternalDispatchAdapter::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
	USES_CONVERSION_EX;
	return E_NOTIMPL ; 
}

//////////////////////////////////////////////////////////////////////////
// CCustomSiteMainFrmAdapter

CHelpCustomSiteAdapter::CHelpCustomSiteAdapter(CWebContainer* pWebContainer)
: CCustomSite(pWebContainer)
{
	m_pExternalDispatch = new CHelpExternalDispatchAdapter(pWebContainer);
	m_pExternalDispatch->AddRef();
}

CHelpCustomSiteAdapter::~CHelpCustomSiteAdapter()
{
	if (m_pExternalDispatch != NULL)
	{
		m_pExternalDispatch->Release();
		m_pExternalDispatch = NULL;
	}
}

STDMETHODIMP CHelpCustomSiteAdapter::GetExternal(IDispatch **ppDispatch)
{
	if (m_pExternalDispatch)
	{
		m_pExternalDispatch->AddRef() ;
		*ppDispatch = m_pExternalDispatch;

		return S_OK ;
	}

	return E_NOTIMPL ;
}

STDMETHODIMP CHelpCustomSiteAdapter::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
{
	if (!ppvObject)
		return E_INVALIDARG;

	*ppvObject = NULL;

	return E_NOINTERFACE;
}

STDMETHODIMP CHelpCustomSiteAdapter::ShowMessage(HWND hwnd, LPOLESTR lpstrText, LPOLESTR lpstrCaption, DWORD dwType, LPOLESTR lpstrHelpFile, DWORD dwHelpContext, LRESULT *plResult)
{
	USES_CONVERSION_EX;
	CResString<MAX_PATH> strTitle(IDS_APP_TITLE);
	
	*plResult = ::MessageBox(hwnd, OLE2T_EX(lpstrText, 0), strTitle, dwType);

	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
// CHelpWebEventSinkAdapter

CHelpWebEventSinkAdapter::CHelpWebEventSinkAdapter(CWebContainer* pWebContainer)
: CWebEventSink(pWebContainer)
{
}

CHelpWebEventSinkAdapter::~CHelpWebEventSinkAdapter()
{
}

HRESULT CHelpWebEventSinkAdapter::OnNewWindow2(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
	return S_FALSE;
}

HRESULT CHelpWebEventSinkAdapter::OnNewWindow3(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
	return S_OK;
}

HRESULT CHelpWebEventSinkAdapter::OnBeforeNavigate2(REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
	return S_OK;
}

///////////////////////////////////////////////////////////////////////////
// CHelpWebControlExAdapter

CHelpWebControlExAdapter::CHelpWebControlExAdapter(CWebContainer* pWebContainer)
: CWebControlEx(pWebContainer)
{
}

CHelpWebControlExAdapter::~CHelpWebControlExAdapter()
{
}
